//! @Author: DengLibin
//! @Date: Create in 2023-11-07 15:36:42
//! @Description: 接口计时

use axum::{
    body::Body, http::{HeaderValue, Request}, middleware::Next, response::Response
};
use log::debug;
use my_util_lib::date;
/// @Author: DengLibin
/// @Date: Create in 2023-11-07 15:45:34
/// @Description: 计时中间件
pub async fn timing(request: Request<Body>, next: Next) -> Response {
    debug!("-------------------------->middleware:timing start");
    let s = date::get_sys_timestamp_millis();
    let mut response = next.run(request).await;
    debug!("-------------------------->middleware:timing end");
    let e = date::get_sys_timestamp_millis();
    let h = HeaderValue::from_str(format!("{}", (e - s)).as_str());
    if let Ok(h) = h {
        response.headers_mut().append("time-consuming", h);
    }

    // do something with `response`...
    // let o = response.headers().get("Content-Type");
    // if let Some(header_value) = o {
    //     if let Ok(s) = header_value.to_str() {
    //         info!("response Content-Type:{}", s);
    //     }
    // }

    response
}
